﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net;
using System.Reflection;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace OnenetDataPushNdPop
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
          //  InitializeComponent();
        }

        private void PushData_Click(object sender, RoutedEventArgs e)
        {
            NewDev();
            createDataFlow();
            //Get();
        }
        public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入

        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }
        private void Get()//连接服务器并获取数据
        {

            //string url = "http://api.heclouds.com/devices/628551504/datapoints?";//设备地址
            string url = "http://api.heclouds.com//bindata/628551504_1600223928237_ir";//设备地址
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            SetHeaderValue(request.Headers, "api-key", "5N5VzjGqkLu3TXdGhpJcvO5SdHE=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            request.ContentType = "application/octet-stream";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            byte[] retData = Encoding.UTF8.GetBytes(retString);
            myStreamReader.Close();
            myResponseStream.Close();
            for (int i = 0; i < retData.Length; i++)
            {
                ///textBox5.Text += retData[i].ToString() + " ,";
            }
            
        }

        public void Post()
        {
            //string url = "http://api.heclouds.com/devices/628551504/datapoints?";
            string url = "http://api.heclouds.com/bindata?device_id=628551504&datastream_id=ir";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "5N5VzjGqkLu3TXdGhpJcvO5SdHE=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            byte[] kwpData = { 0x02, 0x21, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x00};
            string kwpDataStr = System.Text.Encoding.Default.GetString(kwpData);
            string Cod = "{\"datastreams\":[{\"id\":\"kwp2000\",\"datapoints\":[{\"stdid\":\"" + "0x784" + "\"}, {\"data\":\"" + kwpDataStr + "\"}]}]}]}";
            //string Cod = "{\"id\":\"KWP2000\"," + "\"data\":" + "\"" + kwpDataStr + "\"" + "}";
            byte[] data = { 0x02, 0x21, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x00 };//Encoding.UTF8.GetBytes(Cod);
            //request.ContentLength = data.Length;
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
               //extbox1.Text = reader.ReadToEnd();
               //extbox1.Text += "\n";
               //extbox1.Text += Cod;
                //pictureBox1.BackColor = Color.Lime;
            }
        }

        public void NewDev()
        {
            string url = "http://api.heclouds.com/devices";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "4oJhh6NdlNJRwxrWulCDjfGazDw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"title\":\"mqtt_device\",\"desc\":\"test mqtt\",\"private\":true,\"protocol\":\"MQTT\",\"auth_info\":\"HuangQilong\",}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
               // Textbox1.Text = reader.ReadToEnd();
               // Textbox1.Text += "\n";
               // Textbox1.Text += Cod;
                //pictureBox1.BackColor = Color.Lime;
            }
        }

        public void RegDev()
        {
            string url = "http://api.heclouds.com/register_de";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "4oJhh6NdlNJRwxrWulCDjfGazDw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"title\":\"mqtt_device\",\"desc\":\"test mqtt\",\"private\":true,\"protocol\":\"MQTT\",\"auth_info\":\"HuangQilong\",}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
              //  Textbox1.Text = reader.ReadToEnd();
              //  Textbox1.Text += "\n";
              //  Textbox1.Text += Cod;
                //pictureBox1.BackColor = Color.Lime;
            }
        }
        public void createDataFlow()
        {
            string url = "http://api.heclouds.com/devices/628955397/datastreams";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "4oJhh6NdlNJRwxrWulCDjfGazDw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"id\":\"DiagDevFlow\",\"desc\":\"Diagnosis data flow send by device\",}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
              //  Textbox1.Text += "\n";
              //  Textbox1.Text = reader.ReadToEnd();
              //  Textbox1.Text += "\n";
              //  Textbox1.Text += Cod;
                //pictureBox1.BackColor = Color.Lime;
            }
        }
        private void textBox5_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        private void bConnectMqttServer_Click(object sender, RoutedEventArgs e)
        {
            string clientID = "628955397";          //设备ID
            string userName = "371753";             //产品ID
            string passWord = "HuangQilong";        //鉴权信息
           // MqttClient TestClient = new MqttClient("183.230.40.39", Convert.ToInt32("6002"), false, null, null, MqttSslProtocols.None);

            //Get();
        }
    }
}
